package com.hp.gxw.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hp.gxw.cache.CommonCache;
import com.hp.gxw.constant.BaseEntry;
import com.hp.gxw.constant.QueryTypeConstant;
import com.hp.gxw.entity.BaseCommonConstant;
import com.hp.gxw.entity.BaseCommonDataDictionaryAndSet;
import com.hp.gxw.entity.BaseCommonQueryTable;
import com.hp.gxw.service.IBaseCommonConstantService;
import com.hp.gxw.service.IBaseCommonDataDictionaryAndSetService;
import com.hp.gxw.service.IBaseCommonQueryTableService;
import com.hp.gxw.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangping
 * @since 2024-11-11
 */
@Controller
@RequestMapping("/manage/settlement_deliver_info")
public class SettlementDeliverInfoController {

    @Autowired
    public CommonCache commonCache;

    @Autowired
    public IBaseCommonConstantService iBaseCommonConstantService;

    @Autowired
    public IBaseCommonDataDictionaryAndSetService iBaseCommonDataDictionaryAndSetService;

    @Autowired
    public IBaseCommonQueryTableService iBaseCommonQueryTableService;

    /**
     * 主页
     * @return
     */
    @GetMapping("/index/{condition}")
    public String index(@PathVariable("condition") String condition, Model model) throws Exception {
        BaseEntry entry = commonCache.init("settlement_deliver_info");
        /**加载页面table表头*/
        List<BaseCommonDataDictionaryAndSet> colsList = entry.getSetList().stream().filter(item -> item.getDefaultShow()).collect(Collectors.toList());
        List<Map<String,Object>> cols = new ArrayList<>();
        Map<String,Object> checkboxMap = new HashMap<>();
        checkboxMap.put("type", "checkbox");
        cols.add(checkboxMap);
        for(BaseCommonDataDictionaryAndSet col : colsList){
            Map<String,Object> colMap = null;
            if(col.getQueryConstantId() != null){
                List<BaseCommonQueryTable> list = iBaseCommonQueryTableService.list(new LambdaQueryWrapper<BaseCommonQueryTable>()
                        .eq(BaseCommonQueryTable::getDictionaryId, col.getId()));
                for(BaseCommonQueryTable queryTable : list){
                    BaseCommonDataDictionaryAndSet set = iBaseCommonDataDictionaryAndSetService.getOne(new LambdaQueryWrapper<BaseCommonDataDictionaryAndSet>()
                            .eq(BaseCommonDataDictionaryAndSet::getConstantId, col.getQueryConstantId()).eq(BaseCommonDataDictionaryAndSet::getTableField, queryTable.getQueryField()));
                    colMap = new HashMap<>();
                    colMap.put("field", col.getEntityField()+"_"+queryTable.getQueryField());
                    colMap.put("title", set.getFieldExplain());
                    colMap.put("minWidth", set.getFieldExplain().length()<3?80:set.getFieldExplain().length()<5?120:set.getFieldExplain().length()<7?140:150);
                    cols.add(colMap);
                }
            }else{
                colMap = new HashMap<>();
                colMap.put("field", col.getEntityField());
                colMap.put("title", col.getFieldExplain());
                colMap.put("minWidth", col.getFieldExplain().length()<3?80:col.getFieldExplain().length()<5?120:col.getFieldExplain().length()<7?140:150);
                cols.add(colMap);
            }
        }
        /**加载页面table查询条件*/
        List<BaseCommonDataDictionaryAndSet> criteriaList = entry.getSetList().stream().filter(item -> (item.getQueryCriteria() != null && item.getQueryCriteria())).collect(Collectors.toList());
        List<Map<String,Object>> dataFields = new ArrayList<>();
        Map<String, List<Map>> querySelectlist = new HashMap<>();
        for(BaseCommonDataDictionaryAndSet criteria : criteriaList){
            Map<String,Object> criteriaMap = new HashMap<>();
            criteriaMap.put("field", criteria.getTableField());
            criteriaMap.put("title", criteria.getFieldExplain());
            if(criteria.getQueryCriteriaType().equals(QueryTypeConstant.文本框.getCode())){
                criteriaMap.put("edit", "text");
            }else if(criteria.getQueryCriteriaType().equals(QueryTypeConstant.枚举下拉框.getCode())){
                criteriaMap.put("ops", "equal");
                criteriaMap.put("edit", "select");
                String[] split = criteria.getEnumList().split(";");
                List<Map> listMap = new ArrayList<>();
                for(String s : split){
                    Map m = new HashMap();
                    m.put("label",s.split("-")[0]);
                    m.put("value",s.split("-")[1]);
                    listMap.add(m);
                }
                querySelectlist.put(criteria.getEntityField()+"Templet", listMap);
                criteriaMap.put("templet", "#"+criteria.getEntityField()+"Templet");
            }else if(criteria.getQueryCriteriaType().equals(QueryTypeConstant.联表下拉框.getCode())){
                criteriaMap.put("ops", "equal");
                criteriaMap.put("edit", "select");
                BaseCommonConstant constant = iBaseCommonConstantService.getById(criteria.getQueryConstantId());
                BaseEntry queryEntry = commonCache.init(constant.getUrl());
                List<Object> list = queryEntry.getIService().list();
                List<Map> listMap = new ArrayList<>();
                for(Object o : list){
                    Map m = new HashMap();
                    m.put("label", BeanUtil.getMethod(o,criteria.getAssociatedFiled()).toString());
                    m.put("value",BeanUtil.getMethod(o, StrUtil.toCamelCase(criteria.getQueryTableList().get(0).getQueryField())));
                    listMap.add(m);
                }
                querySelectlist.put(criteria.getEntityField()+"Templet", listMap);
                criteriaMap.put("templet", "#"+criteria.getEntityField()+"Templet");
            }
            dataFields.add(criteriaMap);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("url", "sale_order_details");
        map.put("condition", condition);
        map.put("cols", cols);
        map.put("dataFields", dataFields);
        model.addAttribute("obj", map);
        model.addAttribute("querySelectlist", querySelectlist);
        return "settlement_deliver_info/index";
    }

}

